//
//  ZZLeaveDetailCell.m
//  QQplayground
//
//  Created by zhangxy on 2018/8/24.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ZZLeaveDetailCell.h"
#import "UserInfoController.h"

@implementation ZZLeaveDetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    // Initialization code
    _imgAvatar.layer.cornerRadius = 22.0f;
    _imgAvatar.layer.masksToBounds = YES;
    
    
    [_labName setFont:FontFiftent];
    [_labName setTextColor:UIColorFromRGB(TextSizeOneColor)];
    
    [_btnLevel.titleLabel setFont:ListElevenFont];
    _btnLevel.layer.cornerRadius = 4.0f;
    _btnLevel.layer.masksToBounds = YES;
    _btnLevel.userInteractionEnabled = NO;
    _btnVip.userInteractionEnabled = NO;
    [_btnLevel setTitleColor:UIColorFromRGB(TextSizeOneColor) forState:0];
    [_btnVip setTitle:@"" forState:0];
    
    [_labTime setFont:FontThirteen];
    [_labTime setTextColor:UIColorFromRGB(BgLineColor)];
    
    
    [_labContent setFont:FontFiftent];
    [_labContent setTextColor:UIColorFromRGB(TextSizeOneColor)];
    _labContent.numberOfLines = 0;
    
    [_btnTop setBackgroundColor:UIColorFromRGB(BgTitleColor)];
    [_btnTop setTitle:@"Top" forState:0];
    [_btnTop setTitleColor:UIColorFromRGB(TextWhiteColor) forState:0];
    _btnTop.layer.cornerRadius = 5.0f;
    [_btnTop.titleLabel setFont:FontThirteen];
    _btnTop.layer.masksToBounds = YES;
    
    
    [_imgLine setBackgroundColor:UIColorFromRGB(BgSystemColor)];
    
    
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    
    _imgAvatar.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClick:)];
    [_imgAvatar addGestureRecognizer:tap1];
    
    _labName.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClick:)];
    [_labName addGestureRecognizer:tap2];
    
    
    _btnControl.userInteractionEnabled = YES;
    [_btnControl addTarget:self action:@selector(onCotrolClick:) forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)onCotrolClick:(UIButton *) btn{
    if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
        [self.delegate onCellItemClick:ZZClickMore obj:self.tempModel];
    }
}

-(void)avatarClick:(UITapGestureRecognizer *) tap{
    UserInfoController *vc = [[UserInfoController alloc] init];
    vc.user_id = _tempModel.user_info.user_id;
    if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
        [self.delegate onCellItemClick:ZZClickOpenVC obj:vc];
    }
}


-(void)dataToView:(ZZLeaveEntity *) topic{
    _tempModel = topic;
    [_imgAvatar sd_setImageWithURL:[NSURL URLWithString:convertToString(topic.user_info.user_avatar)] placeholderImage:[UIImage imageNamed:@"default_image"]];
    [_labName setText:topic.user_info.user_name];
    
    //设置用户等级显示
    [self setBtnLevelColor:topic.user_info];
    
    if ([topic.user_info.is_vip intValue] == 1){
        [_btnVip setImage:[UIImage imageNamed:@"vip"] forState:0];
        [_labName setTextColor:[UIColor colorWithHexString:@"#FF70A6" alpha:1.0]];
    }
    [_labTime setText:topic.add_time];
    
    if([topic.is_top boolValue]){
        _btnTop.hidden = NO;
    }else{
        _btnTop.hidden = YES;
    }
    
    _labContent.attributedText = [topic.message expressionFormatReplace];
    [_labContent setBackgroundColor:UIColor.clearColor];
    [self autoWidthOfLabelHegith:_labContent with:ScreenWidth - 40];
    
    CGRect lf = _imgLine.frame;
    lf.origin.y = CGRectGetMaxY(_labContent.frame)+15;
    _imgLine.frame = lf;
    
    [self setFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(_imgLine.frame))];
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    CGRect lf = _btnLevel.frame;
    lf.origin.x = CGRectGetMaxX(_labName.frame) + 5;
    lf.size.width = 46;
    _btnLevel.frame = lf;
    [_btnVip setFrame:CGRectMake(CGRectGetMaxX(lf) + 5, lf.origin.y - 2, 52, 18)];

//    CGRect linef = _imgLine.frame;
//    linef.origin.y = CGRectGetMaxY(_labContent.frame)+15;
//    _imgLine.frame = linef;
//
//    [self setFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(_imgLine.frame))];
}

/**
 计算Label高度
 
 @param label 要计算的label，设置了值
 @param width label的最大宽度
 */
- (CGSize )autoWidthOfLabel:(UILabel *)label with:(CGFloat )height{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(FLT_MAX, height);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.width = expectedLabelSize.width;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}


//用户等级数的显示问题
-(void)setBtnLevelColor:(User *) user {
    int level = [user.group_icon intValue];
    if (level >= 100){
        CGRect lf = _btnLevel.frame;
        lf.size.width = 75;
        _btnLevel.frame = lf;
        [_btnVip setFrame:CGRectMake(CGRectGetMaxX(lf) + 5, lf.origin.y - 2, 52, 18)];
        if (level == 100){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"荣誉会员"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"荣誉会员"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level == 101){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"官方团队"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"官方团队"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level == 403){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"1"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"系统屏蔽"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"系统屏蔽"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#A4A4A4" alpha:1.0] forState:0];
        } else if (level == 102){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"小组长"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"小组长"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level == 200){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"原创达人"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"原创达人"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#8D6FFF" alpha:1.0] forState:0];
        } else if (level == 201){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"专题小编"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"专题小编"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#8D6FFF" alpha:1.0] forState:0];
        }
    }
    
    else {
        //等级显示处理
        CGRect lf = _btnLevel.frame;
        [self autoWidthOfLabel:_labName with:21];
        lf.origin.x = CGRectGetMaxX(_labName.frame) + 5;
        lf.size.width = 46;
        _btnLevel.frame = lf;
        [_btnVip setFrame:CGRectMake(CGRectGetMaxX(lf) + 5, lf.origin.y - 2, 52, 18)];
        if (level >= 1 && level < 6){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"1"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 1"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv 0%@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#A4A4A4" alpha:1.0] forState:0];
        } else if (level >= 6 && level < 11) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"2"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 2"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#1BC156" alpha:1.0] forState:0];
        } else if (level >= 11 && level < 16) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"3"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 3"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#4A90E2" alpha:1.0] forState:0];
        } else if (level >= 16 && level < 21) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 4"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#8D6FFF" alpha:1.0] forState:0];
        } else if (level >= 21 && level < 26) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"5"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 5"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF9300" alpha:1.0] forState:0];
        } else if (level >= 26 && level < 31) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"5"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 6"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF9300" alpha:1.0] forState:0];
        } else if (level >= 31 && level < 36) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"7"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 7"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF70A6" alpha:1.0] forState:0];
        } else if (level >= 36 && level < 41) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"8"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 8"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#3785FF" alpha:1.0] forState:0];
        } else if (level >= 41 && level < 46) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 9"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#962BFF" alpha:1.0] forState:0];
        } else if (level >= 46 && level < 51) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 10"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level >= 51 && level < 56) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"11"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 11"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF70A6" alpha:1.0] forState:0];
        } else if (level >= 56 && level < 100) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"12"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 12"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",user.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFFFFF" alpha:1.0] forState:0];
        }
    }
    
}
- (CGSize )autoWidthOfLabelHegith:(UILabel *)label with:(CGFloat )width{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(width, FLT_MAX);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.height = expectedLabelSize.height;
    newFrame.size.width = width;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
